/**
 * StaticController.java
 */
package org.yate.oa.controller;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.yate.oa.exception.FtpOperateException;
import org.yate.oa.model.FtpContent;
import org.yate.oa.service.FtpContentService;
import org.yate.oa.utils.Constants;
import org.yate.oa.utils.FileSuffixEnum;
import org.yate.oa.utils.FtpPathEnum;

/**
 * @author Yate
 * @date Jul 25, 2014
 * @description TODO
 * @version 1.0
 */
@Controller
@RequestMapping("/static")
public class StaticController extends FtpController<FtpContent> {
	@Resource
	protected FtpContentService ftpContentService;
	@Resource
	private FtpController.IWrapperFactory<FtpContent> ftpFactory;

	@Override
	protected IWrapperFactory<FtpContent> getFactory() {
		return ftpFactory;
	}

	@RequestMapping(value = "/text/upload", method = RequestMethod.POST)
	public void upload(MultipartHttpServletRequest req, HttpServletResponse resp) {
		PrintWriter pw = null;
		try {
			MultipartFile f = req.getFile("file");
			pw = resp.getWriter();
			if (f != null) {
				if (!this.validateFile(f, FileSuffixEnum.BMP,
						FileSuffixEnum.JPEG, FileSuffixEnum.JPG,
						FileSuffixEnum.PNG)) {
					pw.write("0");
					pw.flush();
					return;
				}
				if (f.getSize() > Constants.FILE_MAX_SIZE) {
					pw.write("0");
					pw.flush();
					return;
				}

				FtpContent t = this.ftpContentService.store(f.getBytes(),
						wrapper(f, FtpPathEnum.Temp.Path));
				pw.write(t.getId().toString());
			} else {
				pw.write("0");
			}
			pw.flush();
		} catch (IllegalStateException e) {
			log.error(e.getMessage(), e);
			pw.write("0");
		} catch (IOException e) {
			log.error(e.getMessage(), e);
			pw.write("0");
		} catch (FtpOperateException e) {
			log.error(e.getMessage(), e);
			pw.write("0");
		} finally {
			if (pw != null) {
				pw.close();
			}
		}
	}

	@RequestMapping(value = "/source/{id}", method = RequestMethod.GET)
	public void load(@PathVariable long id,
			@RequestParam(value = "w", required = false) Integer w,
			@RequestParam(value = "h", required = false) Integer h,
			HttpServletRequest req, HttpServletResponse resp) {
		FtpContent f = ftpContentService.getEntity(id);
		String name = f.getMd5();
		if (f == null) {
			return;
		}

		InputStream is = null;
		OutputStream os = null;
		try {
			is = FileUtils.openInputStream(new File(ftpRootDir + f.getKey()
					+ File.separator + f.getValue()));
			os = resp.getOutputStream();
			resp.setContentType("application/x-download");// 定义输出类型  
			resp.addHeader("Content-Disposition","attachment;filename=" + name+".pdf");
			if (StringUtils.isNotBlank(f.getType()))
				resp.setContentType(f.getType());
			resp.setCharacterEncoding(Constants.CHARACTOR_CODE);

			resp.setContentLength(f.getLength().intValue());
			IOUtils.copy(is, os);
			os.flush();
		} catch (IOException e) {
			log.error(e.getMessage(), e);
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		} finally {
			IOUtils.closeQuietly(is);
		}
	}
}
